var lon;
var lat;
var reg;
var dat;
var mag;
var dist;
var mp;
var alreadyLoaded = false;

function ScnMappingAssistant(argFromPusher) {
	lon = argFromPusher.lon;
	lat = argFromPusher.lat;
	mag = argFromPusher.mag;
	dep = argFromPusher.dep;
	reg = argFromPusher.reg;
	dat = argFromPusher.dat;
	src = argFromPusher.src;
	dist = argFromPusher.dist;
	showInfo = argFromPusher.showInfo;
}

ScnMappingAssistant.prototype = {
	setup: function() {
		Ares.setupSceneAssistant(this);
		
		this._mapTypeMenuModel = {
			toggleCmd:'back',
			items: [
				{label: $L('Terrain'), command:'do-terr'},
				{label: $L('Hybrid'), command:'do-hyb'},
				{label: $L('Satellite'), command:'do-sat'},
				{label: $L('Roadmap'), command:'do-map'}
			]
		};
		this.cmdMenuModel = {
			
			items: [ 
					{label: $L('Minus'), command:'do-zoom-out', iconPath: "images/zoom_out4.png"},
					{label: "X", items: [
							{label: $L('Map Type'), submenu:'_mapTypeMenu'},
							{label: $L('Share'), command:'do-send'} //, icon: "send"}
						]
					},
					{label: $L('Plus'), command:'do-zoom-in', iconPath: "images/zoom_in4.png"}
			]
		};
		this.controller.setupWidget(Mojo.Menu.commandMenu, this.attributes = {menuClass: 'no-fade'}, this.cmdMenuModel);
		this.controller.setupWidget('_mapTypeMenu', undefined, this._mapTypeMenuModel);
		
		var latlon = new google.maps.LatLng(lat, lon);
		this.mp = this.$.map1.map;
		this.mp.setCenter(latlon);
		myMarker = this.$.map1.centerMarker;
		try {
/*
			if (mag <= 5.0) {
				imgPath = "images/markers/marker-blue.png";
			}
			else if (mag <= 6.0) {
				imgPath = "images/markers/marker-orange.png";
			}
			else if (mag > 6.0) {
				imgPath = "images/markers/marker-red.png";
			}
			// Build Marker
			var myMarker = new google.maps.Marker({
				position : latlon,
				// icon: imgPath,
				map: this.mp
			}); //end myMarker
//			myMarker.setMap(this.mp);
*/			
			// Build InfoHeader or Infowindow
			var cont = "<DIV class='bubble-l'>" + reg + "</DIV>";
			cont = cont + "<DIV class='bubble-m'>"+ $L("Magnitude") + ":&nbsp;" + mag + "</DIV>";
			cont = cont + "<DIV class='bubble-s'>"+ $L("Depth") + ":&nbsp;" + dep + ", " + $L("Dist") + ":&nbsp;" + dist + ",</DIV>";
			cont = cont + "<DIV class='bubble-s'>"+ $L("Date/Time (UTC)") + ":&nbsp;" + dat;
			var iw = new google.maps.InfoWindow({content: cont, position: latlon, disableAutoPan: false});
			
			google.maps.event.addListener(myMarker,"click",(function(){
				iw.open(this.mp, myMarker);
			}).bind(this));
			
/*			var imgPath;
			if (mag <= 5) {imgPath = "images/pinGreen.png";}
			else if (mag <= 6) {imgPath = "images/pinYellow.png";}
			else if (mag <= 7) {imgPath = "images/pinOrange.png";}
			else if (mag > 7)  {imgPath = "images/pinRed.png";}
			var img = new google.maps.MarkerImage(imgPath);
			var mark = new google.maps.Marker({title: mag, position: pos, map: mp, icon: img});
*/
			if ((showInfo == "B") || (showInfo == undefined))  {
				iw.open(this.mp, myMarker);
//				setTimeout(function () { iw.close(); }, 10000);
			}
			else if (showInfo == "H") {
				this.$.hdrMap.setTitle(reg.truncate(30, '...'));
				this.$.hdrMap.setSubtitle($L("Mag") + ": " + mag + "&nbsp;/&nbsp;" + $L("Depth") + ": " + dep + "&nbsp;/&nbsp;" + $L("Dist") + ": " + dist +"<br>" + $L("Date/Time (UTC)") + ": " + dat + " (" + src + ")");
				this.$.hdrMap.show();
			}
			else {
				this.$.hdrMap.hide();
			}
		}
		catch (err) {
			Mojo.Log.error(" ### Could not connect to Google Maps API.", err.message);
			Mojo.Controller.errorDialog($L({value: "Cannot connect to Google Maps! Please ensure you are connected to the internet.", key: "noGooleMapsAPI"}));
		}
	},

	handleCommand: function(event) {
		if (event.type === Mojo.Event.command) {
			if (event.command == 'do-zoom-out') { 
				if (this.mp.getZoom() >= 1) {this.mp.setZoom(this.mp.getZoom() - 1);}
			}
			if (event.command == 'do-zoom-in') { 
					this.mp.setZoom(this.mp.getZoom() + 1);
			}
			if (event.command == 'do-map') { 
					this.mp.setMapTypeId(google.maps.MapTypeId.ROADMAP);
			}
			if (event.command == 'do-hyb') { 
					this.mp.setMapTypeId(google.maps.MapTypeId.HYBRID);
			}
			if (event.command == 'do-sat') { 
					this.mp.setMapTypeId(google.maps.MapTypeId.SATELLITE);
			}
			if (event.command == 'do-terr') { 
					this.mp.setMapTypeId(google.maps.MapTypeId.TERRAIN);
			}
		}
	},

	cleanup: function() {
		this.controller.stageController.parentSceneAssistant(this).needRefresh = false;
		Ares.cleanupSceneAssistant(this);
	},
	hdrMapTap: function(inSender, event) {
		this.$.hdrMap.hide();
	}
};